package com.thenie.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.Menu;
import com.thenie.pojo.Role;
import com.thenie.service.MenuService;
import com.thenie.service.RoleService;
import com.thenie.utils.DataGridViewResult;
import com.thenie.utils.SystemConstant;
import com.thenie.utils.TreeNode;
import com.thenie.vo.RoleVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;

    /**
     * 返回当页角色列表
     * @param roleVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(RoleVo roleVo){
        //设置当前页，当前页的数量大小
        PageHelper.startPage(roleVo.getPage(),roleVo.getLimit());

        //查询部门列表
        List<Role> roleList = roleService.findRoleList(roleVo);

        PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleList);
        return new DataGridViewResult(rolePageInfo.getTotal(), rolePageInfo.getList());
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/addRole")
    public String addRole(Role role){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roleService.addRole(role);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequestMapping("/updateRole")
    public String updateRole(Role role){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roleService.updateRole(role);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping("/deleteRole")
    public String deleteRole(int roleId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i =roleService.deleteRoleById(roleId);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 初始化分配菜单树
     */
    @RequestMapping("/initMenuTree")
    public DataGridViewResult initMenuTree(int roleId){
        //创建带有树节点的菜单集合
        List<TreeNode> treeNodeList =new ArrayList<TreeNode>();
        //查询所有菜单
        List<Menu> menuList = menuService.findAllMenuList();
        List<Integer> menuIdList = menuService.findMenuIdListByRoleId(roleId);

        //移植
        for (Menu menu : menuList) {
            Integer spread = menu.getSpread();//数字表示的是否展开
            int id=menu.getId();//菜单的id
            String checkArr = "0";//复选框是否被选中
            for (int menuId : menuIdList) {
                if(id==menuId){
                    checkArr="1";
                    break;
                }
            }
            Boolean s=(spread==null||spread==1)?true:false;
            treeNodeList.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), s,checkArr));
        }
        return new DataGridViewResult(treeNodeList);
    }
    @RequestMapping("/saveRoleMenu")
    public String saveRoleMenu(String menuIdList,Integer roleId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        String[] menuIds = menuIdList.split(",");//将字符串以","分离
        int code = roleService.saveRoleMenu(roleId, menuIds);
        if(code>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"分配成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"分配失败");
        }
        return JSON.toJSONString(map);
    }
}
